#include "rectangleH.h"
#include "Point.h"
#include <iostream>

using namespace std;


void gotoxy(int, int);
void clrscr();


void Rectangl::init()
{
		m_pointX = 0;
	    m_pointY = 0;
	    m_sizeX = 0;
	    m_sizeY = 0;
        
		system("cls");
		setPos();
		getPos(m_pointX, m_pointY);
		setSize();
		setSymbolRectangle('*');

		calcRectanglePoint(); // Initialize All Rectangle Points TL, TR, BR, BL
		
		
}
void Rectangl::getPointTL(Point * p)
{
	p = &m_TL;
}
void Rectangl::getPointTR(Point * p)
{
	p = &m_TR;
}
void Rectangl::getPointBL(Point * p)
{
	p = &m_BL;
}
void Rectangl::getPointBR(Point * p)
{
	p = &m_BR;
}

void Rectangl::calcRectanglePoint(){
	int x,y;
	getPos(m_pointX,m_pointY);
	m_TL = m_p;
	m_TL.getPos(x,y);
	m_TR.setPos(x+m_sizeX,y);
	m_BL.setPos(x,y+m_sizeY);
	m_BR.setPos(x+m_sizeX,y+m_sizeY);
}

bool Rectangl::isEqual(Rectangl * rec){
	if (rec->m_TL.isEqual(m_TL) && rec->m_sizeX == m_sizeX && rec->m_sizeY == m_sizeY) return true;
	return false;
}

bool Rectangl::isOverlap(Rectangl * rec){

	int x,y,x2,y2,sizeX,sizeY,sizeX_2,sizeY_2;
	bool overlap = false;	
	m_TL.getPos(x,y);
	sizeX = m_sizeX;
	sizeY = m_sizeY;

	rec->m_TL.getPos(x2,y2);
	sizeX_2 = rec->m_sizeX;
	sizeY_2 = rec->m_sizeY;

	if (x2 + sizeX_2 < x) 
    return false;  // rec 2 is left of rec 1

	if (x + sizeX < x2)
    return false;  // rec 1 is left of rec 2

	//4 cases
	
   if (x >= x2 && x <= x2+sizeX_2 && y >= y2 && y <= y2+sizeY_2)
      overlap = true;

   if (x+sizeX >= x2 && x+sizeX <= x2+sizeX_2 && y >= y2 && y <= y2+sizeY_2)
      overlap = true;

   if (x >= x2 && x<= x2+sizeX_2 && y+sizeX >= y2 && y+sizeY <= y2+sizeY_2)
      overlap = true;

   if (x+sizeX >= x2 && x+sizeX <= x2+sizeX_2 && y+sizeY >= y2 && y+sizeY <= y2+sizeY_2)
      overlap = true;

   return overlap;
	
}
void Rectangl::setPos() //Set coordinations
{
	int x, y;	
	
	    cout << "Please enter the X coordination" << endl;
		cin >> x;

		cout << "Please enter the Y coordination" << endl;
		cin >> y;	

		m_p.setPos(x,y);
		if(m_sizeX > 0 && m_sizeY > 0)
			calcRectanglePoint();
}

//Comment:
//Added resetPos Func since if you want to move the rectangle you will not give it new Point(x,y) else a deviation and for that
//i had to change the private data memeber, so set is the right way to do it without asking the use to re-enter coordiantes for rectangle

void Rectangl::resetPos(int x, int y){
	
	if(m_sizeX > 0 && m_sizeY > 0){
		m_p.setPos(x,y);
		calcRectanglePoint();
	}
	else
	{
		cout << "ERROR: Rectangle size is 0, use setPos() to initialize the rectangle."<< endl;
	}
	
}

void Rectangl::getPos(int &x, int &y) //Pulls out the index of x and y parameters and assign
{
	m_p.getPos(x,y);
}

void Rectangl::setSize() //Set size of X and Y intervals (should be valid input)
{
	cout << "Please enter the vertical x size" << endl;
		cin >> m_sizeX;

		if(m_sizeX <= 0)
		{
			   while(m_sizeX <= 0)
			   {
				   cout << "Please enter valid whole vertical x size" << endl;
		           cin >> m_sizeX;
			   }
		}

		cout << "Please enter the vertical y size" << endl;
		cin >> m_sizeY;


		if(m_sizeY <= 0)
		{

			   while(m_sizeY <= 0)
			   {
				   cout << "Please enter valid whole vertical y size" << endl;
		           cin >> m_sizeY;
			   }
		}
}




void Rectangl::setSymbolRectangle(char c) // set symbol
{
	  m_symb = c;
}


void Rectangl::drawPoint(char c) //draw point
{
	m_p.draw(c);
}


void Rectangl::goAndDrawRectangle(int drawingOption) 
{
	if (!m_sizeX || !m_sizeY)
	{
		cout << "ERROR: Go back to class - Rectangl::setSize() is not initialized,\nPlease Initialize! [i.e. Rectangl::setSize()]\n" << endl;
		setSize();

	}
	if(m_pointX+m_sizeX<0 || m_pointX>79) return;
	if(m_pointY+m_sizeY<0 || m_pointY>24) return;

	if (drawingOption != 1)
		drawRectangle2();
	else
		drawRectangle1();

}




void Rectangl::drawRectangle1() // draw the Rectangle
{
	    int tempm_sizeX, tempm_sizeY;
		int tempPosX, tempPosY;
		
		int i;

		tempPosX = m_pointX;
		tempPosY = m_pointY;

		tempm_sizeX = m_sizeX;
		tempm_sizeY = m_sizeY;

		if(tempPosX<0)
		{
				tempm_sizeX=tempm_sizeX+tempPosX;
				tempPosX=0;
		}
		
		if(tempPosY<0)
		{
				tempm_sizeY=tempm_sizeY+tempPosY;
				tempPosY=0;
		}
		
		//Print MOST_UP---MOST_DOWN and go one column right		
		for(i=0; i<tempm_sizeX && m_pointX+i<=79; i++)
		{
			if(m_pointY>=0)	
			{
				gotoxy(tempPosX+i,tempPosY);
				cout << m_symb;
			}


			if(m_pointY+m_sizeY<=24)
			{ 
	
			  gotoxy(tempPosX+i,tempPosY+tempm_sizeY-1);
			  cout << m_symb;
			}

		 }

		// i are the lines
		for(i=0; i<tempm_sizeY && m_pointY+i<=24; i++)
		{
			if(m_pointX>=0)
			{
			   gotoxy(tempPosX,tempPosY+i);
			   cout << m_symb;
			}
			   if(m_pointX+m_sizeX<=79)
			{ 
				for (int j=0; j<tempm_sizeX;j++){
					if (i>0 && i<tempm_sizeY-1)
					{
						if (j==0 || j==tempm_sizeX-1)
						{
							gotoxy(tempPosX+tempm_sizeX-1,tempPosY+i);
							cout << m_symb;
						}
						else
						{
							gotoxy(tempPosX+tempm_sizeX-1-j,tempPosY+i);
							cout << ' '; //Fill of Rectangle ' '
						}
					}
				}
			}
			
		}

}


void Rectangl::drawRectangle2() // draw the Rectangle
{
	    int tempm_sizeX, tempm_sizeY;
		int tempPosX, tempPosY;
		
		int i;

		tempPosX = m_pointX;
		tempPosY = m_pointY;

		tempm_sizeX = m_sizeX;
		tempm_sizeY = m_sizeY;

		if(tempPosX<0)
		{
				tempm_sizeX=tempm_sizeX+tempPosX;
				tempPosX=0;
		}
		
		if(tempPosY<0)
		{
				tempm_sizeY=tempm_sizeY+tempPosY;
				tempPosY=0;
		}
		
				
		for(i=0; i<tempm_sizeX && m_pointX+i<=79; i++)
		{
			if(m_pointY>=0)	
			{
				gotoxy(tempPosX+i,tempPosY);
				cout << m_symb;
			}


			if(m_pointY+m_sizeY<=24)
			{ 
	
			  gotoxy(tempPosX+i,tempPosY+tempm_sizeY-1);
			  cout << m_symb;
			}

		 }



		for(i=0; i<tempm_sizeY && m_pointY+i<=24; i++)
		{
			if(m_pointX>=0)
			{
			   gotoxy(tempPosX,tempPosY+i);
			   cout << m_symb;
			}
			   if(m_pointX+m_sizeX<=79)
			{ 
	
			  gotoxy(tempPosX+tempm_sizeX-1,tempPosY+i);
			  cout << m_symb;
			}
		}

}



bool Rectangl::pointInRectangle(int x, int y)
{
	if( x >= m_pointX && x < (m_pointX + m_sizeX))
	
	{
		if( y >= m_pointY && y < (m_pointY + m_sizeY))
		
			return true;
	}

	return false;
}